/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../../../../engine/control/XButton");
const { X2DBeta } = require("../../../../engine/graphics/X2DInterface");
const { Scr } = require("../../../../engine/XDefine");
const { XTools } = require("../../../../engine/XTools");
const { CustomManager } = require("../../1/CustomManager");
const { RosBase } = require("./RosBase");

class RosNode extends RosBase {
    constructor(name, pubs, subs, srvs) {
        super(name, "node");
        this.pubs = pubs;
        this.subs = subs;
        this.srvs = srvs;
        //修改代码,重新加载,隐藏,停止
        this.btn_code = new XButton({
            name: "修改代码",
            onClicked: () => {
                let cn = CustomManager.getInstance().searchByName(this.name.substring(1));
                CustomManager.getInstance().editNodeCode(cn);
            },
            onDraw: (offx, offy) => {
                this.renderButton(this.btn_code, offx, offy);
            }
        });
        this.btn_reload = new XButton({
            name: "重新加载",
            onClicked: () => {
                let cn = CustomManager.getInstance().searchByName(this.name.substring(1));
                CustomManager.getInstance().reloadNode(cn);
            },
            onDraw: (offx, offy) => {
                this.renderButton(this.btn_reload, offx, offy);
            }
        });
        this.btn_stop = new XButton({
            name: "停止",
            onClicked: () => {
                let cn = CustomManager.getInstance().searchByName(this.name.substring(1));
                CustomManager.getInstance().stopNode(cn);
            },
            onDraw: (offx, offy) => {
                this.renderButton(this.btn_stop, offx, offy);
            }
        });
    }
    onDraw(offx = 0, offy = 0, locked = false) {
        let x = this.x + offx;
        let y = this.y + offy;
        X2DBeta.fillRect(x, y, this.w, 16, 0xff303030);
        let cn = CustomManager.getInstance().searchByName(this.name.substring(1));
        if (cn) {
            X2DBeta.drawText("自定义节点", 12, x + this.w / 2, y + 8, 1, 1, 0, -2, -2, 0xff7bed9f);
        }
        else X2DBeta.drawText("节点", 12, x + this.w / 2, y + 8, 1, 1, 0, -2, -2, 0xff7bed9f);

        y += 16;
        X2DBeta.fillRect(x, y, this.w, 16, 0xff404040);
        X2DBeta.drawText(this.name, 12, x + 4, y + 8, 1, 1, 0, -1, -2, 0xffe0e0e0);

        if (locked) {
            this.btns = [this.btn_hide];
            if (cn) {
                this.btns.push(this.btn_stop);
            }
            for (let i = 0; i < this.btns.length; i++) {
                this.btns[i].move(this.x + this.w, this.y + i * 16, 60, 14).onDraw(offx, offy);
            }
            if (cn) {
                this.btns.push(this.btn_code, this.btn_reload);
                for (let i = 0; i < 2; i++) {
                    this.btns[i + 2].move(this.x - 60, this.y + i * 16, 60, 14).onDraw(offx, offy);
                }
            }
        }
    }
}

module.exports = {
    RosNode,
};